<?php
/**
 * 失物招领
 */
namespace app\mobile\controller;

use app\portal\model\PortalLostModel;
use app\portal\model\PortalPostModel;
use dragon\SysConfig;

class LostController extends BaseController
{
    // 招领物品列表
    public function index()
    {
        $status = $this->request->param('post_status', 1, 'intval');

        $map = [];
        $map['post_status'] = 1;
        $map['post_type'] = SysConfig::TYPE_LOST;

        $list = PortalPostModel::where($map)->order('is_top desc, published_time desc')->paginate($this->perpage);

        foreach($list as $key => $val) {
            $list[$key]['published_time_format'] = date('m-d', $val['published_time']);
            $list[$key]['post_status_format'] = dict_convert('lost_status', $val['post_status']);
        }

        return $this->success($list);
    }

    // 招领记录
    public function history() {
        $map = [];
        $map['user_id'] = $this->user_id;

        $model = new PortalLostModel();

        $list = $model->order( 'update_time desc')->paginate($this->perpage);

        foreach($list as $key => $val) {
            $list[$key]['update_time_format'] = date('m-d', $val['update_time']);
            $list[$key]['create_time_format'] = date('m-d', $val['create_time']);
            $list[$key]['status_format'] = dict_convert('user_lost_status', $val['status']);

            $list[$key]['article']['published_time_format'] = date('m-d', $val['article']['published_time']);
        }

        return $this->success($list);
    }

    // 招领申请
    public function apply() {
        $id = $this->request->param('id', 0, 'intval');
        $article = PortalPostModel::get($id);

        // 添加招领申请
        PortalLostModel::create([
            'post_id'   => $id,
            'user_id'   => $this->user['id'],
            'user_name' => $this->user['user_realname'],
            'user_mobile'   => $this->user['mobile'],
            'status'    => 1,
        ]);

        // 修改招领状态
        $article->save([
            'post_status' => 2,
        ]);

        return $this->success('申请成功，请等待审核');
    }

    // 招车详情
    public function cancel() {
        $taxiCall = PortalLostModel::get($this->request->param('id', 0, 'intval'));
        $taxiCall->save(['status' => 0]);
        return $this->success('操作成功');
    }

    public function info() {
        $id = $this->request->param('id', 0, 'intval');

        $portalPostModel = new PortalPostModel();
        $info = $portalPostModel->where('id', $id)->find();
        $info = ($info->toArray());
        $info['published_time_format'] = time_format('Y-m-d H:i', $info['published_time']);
        $info['create_time_format'] = time_format('Y-m-d H:i', $info['create_time']);
        $info['update_time_format'] = time_format('Y-m-d H:i', $info['update_time']);
        $info['apply'] = PortalLostModel::where(['post_id' => $info['id']])->order('id desc')->find();
        if($info['apply']) {
            $info['apply']['create_time_format'] = date('Y-m-d H:i', $info['apply']['create_time']);
            $info['apply']['status_format'] = dict_convert('user_lost_status', $info['apply']['status']);
        }

        if($info['more']['photos']) {
            foreach($info['more']['photos'] as $key => $val) {
                $info['more']['photos'][$key]['url'] = dc_get_image_preview_url($val['url']);
            }
        }

        return $this->success($info);
    }
}
